<!-- eslint-disable no-undef -->
<script setup lang="ts">
import { OrderType, type ConsultType } from '@/enums'
import { reqGetConsultList } from '@/services/consult'
import type { ConsultOrderItem } from '@/types/consult'
import ConsultItem from './ConsultItem.vue'
const props = defineProps<{
  type: ConsultType
}>()
// 查询参数
const queryParams = ref({
  current: 1,
  pageSize: 10,
  type: props.type
})
const loading = ref(false) // 记录加载中状态的 默认是false load函数执行会自动变为true 一次请求结束后需要 手动设置为false
const finished = ref(false) // 默认false 数据没有了 设置为true 显示没有更多了
const list = ref<ConsultOrderItem[]>([])
// 数据未占满屏幕会自动执行加载数据, 还未占满 继续执行   滚动到页面距离30 也会自动执行
const onLoad = async () => {
  const res = await reqGetConsultList(queryParams.value)
  list.value.push(...res.data.rows)
  // 手动设置loading为false
  loading.value = false
  if (queryParams.value.current < res.data.pageTotal) {
    queryParams.value.current++
  } else {
    finished.value = true
  }
}
const cancelOrder = (id: string) => {
  // 拿着id找到对应的订单, 将订单的状态改成 取消  status
  const o = list.value.find((item) => item.id === id)
  if (o) {
    o.status = OrderType.ConsultCancel
    o.statusValue = '已取消'
  }
}
const delOrder = (id: string) => {
  // 拿着id找到对应的订单的索引 截取
  const i = list.value.findIndex((item) => item.id === id)
  list.value.splice(i, 1)
}
</script>
<template>
  <div class="consult-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <consult-item
        v-for="item in list"
        :key="item.id"
        :item="item"
        @cancelOrder="cancelOrder"
        @delOrder="delOrder"
      />
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.consult-list {
  padding: 10px 15px;
}
</style>
